#ifndef __M_SINK_H__
#define __M_SINK_H__
#include "unil.hpp"
#include "logs.hpp"
#include "format.hpp"
#include "mags.hpp"
#include "sink.hpp"
#include "logger.hpp"
#include "buffer.hpp"
#include <vector>
#include <unistd.h>
using namespace std;

void test()
{
    TopLog::Logger::ptr logger = TopLog::LoggerManages::GetInstance().getLogger("asnyc_logger"); //获取snyc_logger日志器
    // logger->unknowns(__FILE__, __LINE__, "%s", "测试日志");
    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->warning(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");
    // logger->off(__FILE__, __LINE__, "%s", "测试日志");

    // // size_t now_cur = 0;
    // // 10兆大小  1兆进行切换
    size_t count = 0;
    while (count < 200000)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }

}

int main()
{

    std::unique_ptr<TopLog::LoggerBuilder> builder(new TopLog::GloBalLoggerBuilder()); // 智能指针管理对象
    builder->buildLoggerType(TopLog::LoggerType::LOGGER_ASYNC);
    builder->buildLoggerLevel(TopLog::Logs::Value::WARNING);
    builder->buildLoggerName("asnyc_logger");
    builder->buildLoggerFamtter("%m%n");
    builder->buildSink<TopLog::FileSink>("./log/async.log");
    builder->buildSink<TopLog::StdoutSink>();
    builder->build();
    test();

    // ifstream ifs("./log/text.log", std::ios::binary);
    // if (!ifs.good())
    // {
    //     std::cout << "open file false" << std::endl;
    //     return -1;
    // }
    // ifs.seekg(0, std::ios::end);
    // size_t fsize = ifs.tellg();

    // std::cout << fsize << std::endl;
    // ifs.seekg(0, std::ios::beg);
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);

    // TopLog::Buffer buffer;
    // ofstream oft("./log/tmp.log", std::ios::binary);
    // for (int i = 0; i < body.size(); i++)
    // {
    //     buffer.push(&body[i], 1);
    // }
    // size_t rsize = buffer.readAbleSize();
    // std::cout << rsize << std::endl;
    // // 单字节写入
    // for (int i = 0; i < rsize; i++)
    // {
    //     oft.write(buffer.begin(), 1);
    //     buffer.moveReader(1);
    // }
    // if (oft.good() == false)
    // {
    //     std::cout << "write data false" << std::endl;
    //     return -1;
    // }

    // TopLog::Logs::Value limit = TopLog::Logs::Value::WARNING;
    // TopLog::Formatter::ptr fmt(new TopLog::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // TopLog::Logsink::ptr stdout_lsp = TopLog::SinkFactory::create<TopLog::StdoutSink>();
    // TopLog::Logsink::ptr file_lsp = TopLog::SinkFactory::create<TopLog::FileSink>("./log/text.log");
    // TopLog::Logsink::ptr roll_lsp = TopLog::SinkFactory::create<TopLog::RollSink>("./log/roll-", 1024 * 1024);
    // std::vector<TopLog::Logsink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
    // TopLog::Logger::ptr logger(new TopLog::SyncLogger(logger_name, limit, fmt, sinks));

    // TopLog::LogMsg msg(10, "root", "test.cc", "返回值错误", TopLog::Logs::Value::INFO);
    // TopLog::Formatter fmt;
    // // TopLog::Formatter fmt;
    // string ss = fmt.format(msg);
    // // cout << ss << endl;

    // // TopLog::Logsink::ptr lsp = TopLog::SinkFactory::create<TopLog::StdoutSink>();
    // // TopLog::Logsink::ptr file_log = TopLog::SinkFactory::create<TopLog::FileSink>("./log/text.log");
    // // TopLog::Logsink::ptr roll_log = TopLog::SinkFactory::create<TopLog::RollSink>("./log/roll-", 1024 * 1024);
    // TopLog::Logsink::ptr roll_time_log = TopLog::SinkFactory::create<RollTimeSink>("./log/roll-", TIMEGAP::SECOND);

    // time_t cur_time = TopLog::Unil::Data::Gettime();

    // while (TopLog::Unil::Data::Gettime() < cur_time + 5)
    // {
    //     roll_time_log->log(ss.c_str(), ss.size());
    //     usleep(1000);
    // }

    // lsp->log(ss.c_str(), ss.size());
    // file_log->log(ss.c_str(), ss.size());

    // size_t cur = 1024 * 1024 * 10;
    // size_t now_cur = 0;
    // // 10兆大小  1兆进行切换
    // size_t count = 0;
    // while (cur >= now_cur)
    // {
    //     std::string tmp = to_string(count++) + ss;
    //     roll_log->log(tmp.c_str(), tmp.size());
    //     now_cur += tmp.size();
    // }

    // TopLog::StdoutSink::ptr stduot_lsp = TopLog::SinkFactory::create<TopLog::StdoutSink>();
    // TopLog::FileSink::ptr file_lsp = TopLog::SinkFactory::create<TopLog::FileSink>("./logfile/test.log");
    // TopLog::RollSink::ptr roll_lsp = TopLog::SinkFactory::create<TopLog::RollSink>("./logfile/roll-", 1024 * 1024);
    // stduot_lsp->log(ss.c_str(), ss.size());
    // file_lsp->log(ss.c_str(), ss.size());
    // int cur_size = 0;
    // int count = 0;
    // while (cur_size < 1024 * 1024 * 10)
    // {
    //     string tmp = ss + to_string(count++);
    //     roll_lsp->log(tmp.c_str(), tmp.size());
    //     cur_size += tmp.size();
    // }

    // cout<<TopLog::Unil::Data::Gettime()<<endl;
    // string pathname ="./GTA/abc/1.exe";

    // TopLog::Unil::File::CreateDirectory(TopLog::Unil::File::Path(pathname));

    // cout<<TopLog::Logs::Tostring(TopLog::Logs::Value::DEBUG)<<endl;

    return 0;
}

#endif